package net.seayan.designpattern4j.junit;

import java.io.File;

import net.seayan.designpattern4j.abstractfactory.IModuleFactory;
import net.seayan.designpattern4j.abstractfactory.factory.PerfectModuleFactory;
import net.seayan.designpattern4j.abstractfactory.factory.PoorModuleFactory;
import net.seayan.designpattern4j.abstractfactory.impl.PerfectFridgeModule;
import net.seayan.designpattern4j.factory.IModule;
import net.seayan.designpattern4j.factory.ModuleFactory;
import net.seayan.designpattern4j.factory.impl.CatModule;
import net.seayan.designpattern4j.factory.impl.DogModule;
import net.seayan.designpattern4j.gate.GateHelper;
import net.seayan.designpattern4j.proxy.impl.BossProxy;
import net.seayan.designpattern4j.proxy.impl.CEOProxy;
import net.seayan.designpattern4j.proxy.impl.Secretary;
import net.seayan.designpattern4j.singleton.SingletonSafe;
import net.seayan.designpattern4j.singleton.SingletonSimple;
import net.seayan.designpattern4j.strategy.context.StrategyContext;
import net.seayan.designpattern4j.strategy.impl.StartServer;
import net.seayan.designpattern4j.strategy.impl.StopServer;
import net.seayan.designpattern4j.template.AbstractTemplate;
import net.seayan.designpattern4j.template.CarImpl;
import net.seayan.designpattern4j.template.JeepImpl;

import org.junit.Test;

public class TestPattern {
	
	// 测试策略模式
		@Test
		public void testStrategy() {
			StrategyContext context;
			
			//测试startserver
			context=new StrategyContext(new StartServer());
			context.mmethod();
			
			//测试stopserver
			context=new StrategyContext(new StopServer());
			context.mmethod();
		}
		
		@Test
		public void testProxy(){
			Secretary secretary;
			secretary=new  Secretary(new CEOProxy());
			secretary.readMail();
			secretary.writeMail();
			
			secretary=new Secretary(new BossProxy());
			secretary.readMail();
			secretary.writeMail();
		}
		
		@Test
		public void testSingleton(){
			SingletonSimple singletonSimple=SingletonSimple.getInstance();
			SingletonSimple singletonSimple2=SingletonSimple.getInstance();
			
			System.out.println(singletonSimple2.equals(singletonSimple));
			
			SingletonSafe singletonSafe=SingletonSafe.getInstance();
			SingletonSafe singletonSafe2=SingletonSafe.getInstance();
			
			System.out.println(singletonSafe.equals(singletonSafe2));
		}
		
		@Test
		public void testFactory(){
			IModule dog=ModuleFactory.createModule(DogModule.class);
			dog.eat();
			dog.run();
			dog.speak();
			
			IModule cat=ModuleFactory.createModule(CatModule.class);
			cat.eat();
			cat.run();
			cat.speak();
			
			IModule module=ModuleFactory.createModule();
			module.eat();
			module.speak();
			module.run();
		}
		
		@Test
		public void testabstractFactory(){
			IModuleFactory perfectFactory=new PerfectModuleFactory();
			IModuleFactory poorFactory=new PoorModuleFactory();
			
			perfectFactory.createFridgeModule().reset();
			perfectFactory.createWasherModule().reset();
			
			poorFactory.createFridgeModule().reset();
			poorFactory.createWasherModule().reset();
		}
		
		@Test
		public void testGate(){
			GateHelper helper=new GateHelper();
			helper.logResult("2012-08-30","AK",new File("temp"));
			
		}
	
	@Test
	public void testTemplate() {
		CarImpl car=new CarImpl();
		AbstractTemplate jeep=new JeepImpl();
		
		car.setHook(true);
		car.run();
		jeep.run();
	}

}
